package com.example.demolearn.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
// 手写一个AOP切面

/**
 * 第一步需要在pom.xml添加依赖spring-boot-starter-aop
 * 切片需要
 */
@Aspect   // 声明是一个切面类
@Component // 引起Spring的注意
public class ControllerAspect {
    @Pointcut(value = "@annotation(com.example.demolearn.aspect.HttpLog)")
    public void pointcut() {

        System.out.println("原方法执行后会执行我！！");
    }

    @Around("pointcut()") // 调用Controller方法的时候会被调用
    public void simpleAop(final ProceedingJoinPoint pjp) throws Throwable{
        Object[] args = pjp.getArgs();
        // 调用原有的方法
        HttpServletRequest request = ((ServletRequestAttributes) Objects
                .requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        System.out.println("接受请求 请求地址：" + request.getRequestURL());
        System.out.println("参数：" + Arrays.toString(args));
        pjp.proceed();
    }
}
